#region Using directives

using System;
using System.Text ;
using System.Data ;
using System.Collections ;

using WP.MF.DataEntity; 
using WP.MF.DataAccess; 
using WP.MF.BusinessRule; 
using WP.DB;
using WP.Commons;

#endregion
namespace WP.MF.BusinessLogical
{

	/*
	 * Created by SmartCreateCode Tool
	 * At 4/22/2005
	 */

    public class MProvinceBLL : IBusinessLogical
	{
		ArrayList multiList=new ArrayList ();
		MProvinceDAL mProvinceDAL =new MProvinceDAL();

		public MProvinceBLL()
		{
        }

        #region ***For implement interface

        #region Select Record by RECID
        public IDataEntity SelectDataByRecID(string strRecID)
        {
            DBAccess dBAccess = new DBAccess(false, false);
            try
            {
                MProvinceDEL mProvinceDEL = mProvinceDAL.SelectDataByRecID(dBAccess, strRecID);
                return mProvinceDEL as IDataEntity;
            }
            catch (Exception e)
            {
                ExceptionManager.ProcessException(e);
                return null;
            }
            finally
            {
                dBAccess.CloseCon();
            }
        }
        #endregion

		#region Insert MProvince
        public bool Insert(DBAccess dBAccess, MProvinceDEL mProvinceDEL)
        {
            bool bFlag = true;
            MProvinceBRL mProvinceBRL = new MProvinceBRL();

            //BaseFunctions.SpecialCharDeal(mProvinceDEL);

            if (mProvinceBRL.InsertValid(dBAccess, mProvinceDEL))
            {
                mProvinceDAL.InsertMProvince(dBAccess, mProvinceDEL);
            }
            else
            {
                bFlag = false;
            }
            return bFlag;

        }

        public bool Insert(IDataEntity iMProvinceDEL)
		{
            bool bReturn = true;
            DBAccess dBAccess = null;
            MProvinceDEL mProvinceDEL = iMProvinceDEL as MProvinceDEL;
			dBAccess=new DBAccess(false,true);

			try
			{
                bReturn = Insert(dBAccess, mProvinceDEL);
                if (bReturn)
                {
                    dBAccess.CommitTranAndCloseCon();
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
			}
			catch(Exception e)
			{
				//RollBackTran trainsaction
				dBAccess.RollbackTran();
                bReturn = false;
                ExceptionManager.ProcessException(e);
			}

            return bReturn;
		}
		#endregion

		#region Update MProvince
        public bool Update(DBAccess dBAccess, MProvinceDEL mProvinceDEL)
        {
            bool bFlag = true;
            MProvinceBRL mProvinceBRL = new MProvinceBRL();

            //BaseFunctions.SpecialCharDeal(mProvinceDEL);

            if (mProvinceBRL.UpdateValid(dBAccess, mProvinceDEL))
            {
                mProvinceDAL.UpdateMProvince(dBAccess, mProvinceDEL);
            }
            else
            {
                bFlag = false;
            }
            return bFlag;
        }

        public bool Update(IDataEntity iMProvinceDEL)
		{
            bool bReturn = true;
            MProvinceDEL mProvinceDEL = iMProvinceDEL as MProvinceDEL;
			DBAccess dBAccess=new DBAccess(false,true);

			try
			{
                bReturn = Update(dBAccess, mProvinceDEL);
                if (bReturn)
                {
                    dBAccess.CommitTranAndCloseCon();
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
			}
			catch(Exception e)
			{
				//RollBackTran trainsaction
				dBAccess.RollbackTran();
                ExceptionManager.ProcessException(e);
                bReturn = false;
			}

            return bReturn;
		}
		#endregion

		#region Delete MProvince
		public bool Delete(string strRecId,byte[] btOldRecStamp)
		{
			MProvinceBRL mProvinceBRL=new MProvinceBRL();
			DBAccess dBAccess=new DBAccess(false,true);

			try
			{	
				if(mProvinceBRL.DeleteValid(dBAccess,strRecId,btOldRecStamp))
				{
					mProvinceDAL.DeleteMProvince(dBAccess,int.Parse(strRecId));
					dBAccess.CommitTranAndCloseCon();

					return true;
				}
				else
				{
					dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
					return false;
				}
			}
			catch(Exception e)
			{
				dBAccess.RollbackTranAndClose();
				ExceptionManager.ProcessException(e);
				return false;
			}
		}

		#endregion

        #endregion***

        #region Select MProvince
        public MProvinceDEL SelectMProvinceByKey(int RecID)
        {
            MProvinceDEL mProvinceDEL = new MProvinceDEL();
            DBAccess dBAccess = new DBAccess();
            return mProvinceDAL.SelectMProvinceByKey(dBAccess, RecID);
        }
        #endregion

        #region Update detail table by DataRow
        public bool UpdateDtl(DataRow dr)
        {
            DBAccess dBAccess = new DBAccess(false, true);
            try
            {
                DataRow drOld = dBAccess.GetDataFirstRow("select top 1 * from  mProvItemRecyclingFee where RecId='" + dr["RecId"].ToString() + "'");
                mProvinceDAL.UpdateProvItemRecyclingFee(dBAccess, dr);
                #region write log
                if (CvtFunc.ToDouble(drOld["FeeAmt"]) != CvtFunc.ToDouble(dr["FeeAmt"]) || CvtFunc.ToDouble(drOld["DepositAmt"]) != CvtFunc.ToDouble(dr["DepositAmt"]))
                {
                    string strLog = "Update a Province detail line Prov# (" + dr["ProvNo"].ToString().TrimEnd() + ") Item# (" + dr["ItemNo"].ToString().TrimEnd() + ") ";
                    if (CvtFunc.ToDouble(drOld["FeeAmt"]) != CvtFunc.ToDouble(dr["FeeAmt"]))
                        strLog += "Recycling Rate " + BaseFunctions.FormatNumeric(drOld["FeeAmt"].ToString(), "ns.2") + " To " + BaseFunctions.FormatNumeric(dr["FeeAmt"].ToString(), "ns.2") + " ";
                    if (CvtFunc.ToDouble(drOld["DepositAmt"]) != CvtFunc.ToDouble(dr["DepositAmt"]))
                        strLog += "Deposit Rate " + BaseFunctions.FormatNumeric(drOld["DepositAmt"].ToString(), "ns.2") + " To " + BaseFunctions.FormatNumeric(dr["DepositAmt"].ToString(), "ns.2") + " ";
                    DataFunctions.AddLog(dBAccess, strLog, "M", "");
                }
                #endregion

                dBAccess.CommitTranAndCloseCon();
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;

            }
            return true;
        }
        #endregion

        #region Insert detail table by DataRow
        public bool InsertDtl(DataRow dr)
        {
            DBAccess dBAccess = new DBAccess(false, true);
            try
            {
                mProvinceDAL.InsertProvItemRecyclingFee(dBAccess, dr);
                #region write log
                string strLog = "Insert a Province detail line Item# (" + dr["ItemNo"].ToString().TrimEnd() + ") in Prov# " + dr["ProvNo"].ToString().TrimEnd();
                DataFunctions.AddLog(dBAccess, strLog, "M", "", "Insert","");
                #endregion

                dBAccess.CommitTranAndCloseCon();
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
            return true;
        }
        #endregion
        #region Delete detail table by Key
        public bool DeleteDtl(int RecID)
        {
            DBAccess dBAccess = new DBAccess(false, true);
            MProvinceBRL mProvinceBRL = new MProvinceBRL();
            try
            {
                DataRow dr = dBAccess.GetDataFirstRow("select top 1 * from  mProvItemRecyclingFee where RecId='" + RecID.ToString() + "'");
                mProvinceDAL.DeleteProvItemRecyclingFee(dBAccess, RecID);
                #region write log
                if (dr != null)
                {
                    string strLog = "Delete a Province detail line Prov# (" + dr["ProvNo"].ToString().TrimEnd() + ") Item# (" + dr["ItemNo"].ToString().TrimEnd() + ")";
                    DataFunctions.AddLog(dBAccess, strLog, "M", "", "Delete", "");
                }
                #endregion

                dBAccess.CommitTranAndCloseCon();

            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
            return true;
        }
        #endregion
	}
}